package com.xy.biz.mes.consumer;

import com.xy.biz.mes.client.ManufactureTaskClient;
import com.xy.biz.mes.domain.enums.ManufactureOrderTypeEnum;
import com.xy.biz.mes.domain.req.SaveManufactureOrderGoodsRequest;
import com.xy.biz.mes.domain.req.SaveManufactureOrderRequest;
import com.xy.biz.mes.domain.resp.GetManufactureResultResponse;
import com.xy.biz.mes.listener.ManufactureTaskListener;
import com.xy.biz.mes.service.ManufactureOrderService;
import com.xy.biz.rs.domain.enums.ReconstitutionStatusEnum;
import com.xy.biz.rs.domain.enums.SplitGoodsTypeEnum;
import com.xy.lang.enums.HandOverResultEnum;
import com.xy.tool.util.BeanUtilsEx;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service("mesManufactureResultConsumer")
public class ManufactureResultConsumer implements InitializingBean, DisposableBean {

    @Autowired
    private ManufactureTaskClient manufactureTaskClient;
    @Autowired
    private ManufactureOrderService manufactureOrderService;

    private ManufactureTaskListenerImpl manufactureResultListener;

    @Override
    public void afterPropertiesSet() throws Exception {
        manufactureResultListener = new ManufactureTaskListenerImpl();
        manufactureTaskClient.addListener(manufactureResultListener);
    }

    @Override
    public void destroy() throws Exception {
        if(manufactureResultListener != null) {
            manufactureTaskClient.removeListener(manufactureResultListener);
        }
    }

    class ManufactureTaskListenerImpl implements ManufactureTaskListener {

        @Override
        public void onSave(Collection<GetManufactureResultResponse> collection) {
            if(!CollectionUtils.isEmpty(collection)) {
                List<SaveManufactureOrderRequest> orderList = new ArrayList<>();
                for(GetManufactureResultResponse acceptanceResult : collection) {
                    if(HandOverResultEnum.TO_SPLIT.equalsTo(acceptanceResult.getHandOverResult())) {
                        SaveManufactureOrderRequest order = new SaveManufactureOrderRequest();
                        orderList.add(order);
                        order.setSourceOrderCode(acceptanceResult.getManufactureResultCode());
                        order.setOrderType(ManufactureOrderTypeEnum.SPLIT.getCode());
                        order.setExpectManufactureDate(LocalDate.now());
                        order.setReconstitutionStatus(ReconstitutionStatusEnum.NO.getCode());
                        List<SaveManufactureOrderGoodsRequest> goodsList = new ArrayList<>();
                        order.setGoodsList(goodsList);
                        acceptanceResult.getManufactureResultGoodsList().forEach(e -> {
                            SaveManufactureOrderGoodsRequest goods = BeanUtilsEx.copyProperties(e, SaveManufactureOrderGoodsRequest::new);
                            goodsList.add(goods);
                            goods.setType(SplitGoodsTypeEnum.BEFORE_SPLIT.getCode());
                            goods.setExpectCount(e.getFactCount());
                            goods.setExpectQuantity(e.getFactQuantity());
                            goods.setFactCount(0);
                            goods.setFactQuantity(0D);
                        });
                    }
                }
                if(!CollectionUtils.isEmpty(orderList)) {
                    manufactureOrderService.saveAllManufactureOrder(orderList);
                }
            }
        }

    }

}
